﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Smart.Utils.Net;
using Smart.Service.Class;
using Smart.Utils.Lib;
using Smart.Utils.App;

namespace Smart.Common.NET
{
    public partial class xfmSendMail : DevExpress.XtraEditors.XtraForm
    {
        private int _action = 0;
        private bool _exit = false;
        private string sContent = "Xin qu\x00fd kh\x00e1ch vui l\x00f2ng nhập nội dung phản hồi.";
        private string sDiaChi = "Xin qu\x00fd kh\x00e1ch vui l\x00f2ng nhập th\x00f4ng tin địa chỉ.";
        private string sDienThoai = "Xin qu\x00fd kh\x00e1ch vui l\x00f2ng nhập th\x00f4ng tin điện thoại.";
        private string sEmail = "Xin qu\x00fd kh\x00e1ch vui l\x00f2ng nhập th\x00f4ng tin email.";
        private string sFail = "Cập nhất thất bại";
        private string sNhapTen = "Xin qu\x00fd kh\x00e1ch vui l\x00f2ng nhập th\x00f4ng tin họ t\x00ean.";
        private string sSubject = "Xin qu\x00fd kh\x00e1ch vui l\x00f2ng nhập ti\x00eau để của y\x00eau cầu.";
        private string sSuccessful = "Cập nhật th\x00e0nh c\x00f4ng.\n\nCảm ơn qu\x00fd kh\x00e1ch đ\x00e3 sử dụng phần mềm của ch\x00fang t\x00f4i.";


        public event SuccessEventHander Success;

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (this.txtCompany.Text == string.Empty)
            {
                this.Notify("Xin qu\x00fd kh\x00e1ch vui l\x00f2ng nhập th\x00f4ng tin c\x00f4ng ty.");
                this.txtCompany.ErrorText = "Not empty";
                this.Err.SetIconAlignment(this.txtCompany, ErrorIconAlignment.MiddleRight);
                this.txtCompany.Focus();
            }
            else if (this.txtAddress.Text == "")
            {
                this.Notify(this.sDiaChi);
                this.txtAddress.Focus();
            }
            else if (this.txtPhoneNumber.Text == "")
            {
                this.Notify(this.sDienThoai);
                this.txtPhoneNumber.Focus();
            }
            else if ((this.txtEmail.Text == "") | !MyLib.IsValidEmailAddress(this.txtEmail.Text))
            {
                this.Notify(this.sEmail);
                this.txtEmail.Focus();
            }
            else if (this.cbxIndustry.Text.Trim() == "")
            {
                this.Notify("Xin qu\x00fd kh\x00e1ch vui l\x00f2ng nhập th\x00f4ng tin ng\x00e0nh nghề kinh doanh.");
                this.cbxIndustry.Focus();
            }
            else if (this.txtContact.Text == "")
            {
                this.Notify(this.sNhapTen);
                this.txtContact.Focus();
            }
            else if (this.mmeContent.Text == "")
            {
                this.Notify(this.sContent);
                this.mmeContent.Focus();
            }
            else
            {
                this.btnSend.Enabled = false;
                CUSTOMER item = new CUSTOMER(7, this.txtCompany.Text, this.txtAddress.Text, this.txtPhoneNumber.Text, "", this.txtFaxNumber.Text, this.txtWebsite.Text, this.txtEmail.Text, "", "", this.txtContact.Text, this.mmeContent.Text);
                this.RaiseSuccessEventHandler(item);
                string subject = MyAssembly.AssemblyTitle.Replace("Phần Mềm", "") + ", Kh\x00e1ch h\x00e0ng " + this.txtContact.Text + ", " + this.txtEmail.Text + ", " + this.mmeContent.Text;
                string str2 = "<br/>";
                string content = ((("\tTh\x00f4ng Tin Kh\x00e1ch H\x00e0ng" + str2) + "|**************************************************************************" + str2) + "|\tFrom: " + MyAssembly.AssemblyTitle + str2) + "|\tHọ t\x00ean:     " + this.txtContact.Text + str2;
                if (this.txtCompany.Text.Trim().Length > 0)
                {
                    content = content + "|\tC\x00f4ng ty:    " + this.txtCompany.Text + str2;
                }
                content = (((((content + "|\tĐịa chỉ:    " + this.txtAddress.Text + str2) + "|\tĐiện thoại: " + this.txtPhoneNumber.Text + str2) + "|\tEmail:      " + this.txtEmail.Text + str2) + "|**************************************************************************" + str2 + str2) + "\tNội dung:" + str2 + str2) + this.mmeContent.Text;
                SendMail mail = new SendMail();
                if (mail.Send2(this.txtEmail.Text, this.txtContact.Text, "support@perfect.com.vn", "Support", subject, content, MyAssembly.AssemblyTitle))
                {
                    this.Infomation(this.sSuccessful);
                    base.Close();
                }
                this.btnSend.Enabled = true;
                if (this._exit)
                {
                    this._exit = false;
                    base.Close();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void xfmSendMail_Load(object sender, EventArgs e)
        {

        }
        private void Notify(string message)
        {
            XtraMessageBox.Show(this, message, "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        private void Infomation(string message)
        {
            XtraMessageBox.Show(this, message, "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void RaiseSuccessEventHandler(CUSTOMER item)
        {
            if (this.Success != null)
            {
                this.Success(this, item);
            }
        }

        private void txtCompany_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtCompany.ErrorText != "")
            {
                this.txtCompany.ErrorText = "";
            }
        }

        private void xfmCompany_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = this._exit;
        }

        public delegate void SuccessEventHander(object sender, CUSTOMER item);
    }
}